package com.hcbd.tdwt.ui.common.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;

import android.widget.TextView;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.BottomBar;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.ui.common.fragment.CommunicationFragment_;
import com.hcbd.tdwt.MainApp;



import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;


@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_tdwt_main)
public class MainActivity extends BaseActivity {
    @ViewById
    BottomBar bottomBar;

    @ViewById
    TextView title;

    @AfterViews
    protected void init(){


        initBottomBar();
        title.setText("北斗通讯助手");
        initGps();
    }

    public void initBottomBar() {
        bottomBar.setContainer(R.id.fl_container)
                .setFirstChecked(0)
                .setTitleBeforeAndAfterColor("#666666", "#02a8f4")
                .addItem(CommunicationFragment_.class,
                        "通信", R.drawable.tabbar_box_status_unchecked_icon,
                        R.drawable.tabbar_box_status_checked_icon).build();
        bottomBar.setOnSwitchListener(currentFragment -> {
            if (currentFragment instanceof CommunicationFragment_) {
                title.setText("北斗通讯助手");
                ((CommunicationFragment_) bottomBar.getCurrentFragment()).onSwitch();
            }
        });
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case Constant.TO_OPEN_GPS_REQUEST_CODE:
                    //开启GPS，重新添加地理监听
                    MainApp.getInstance().initMap();
                    hideWarnDialog();
                    break;
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            switch (requestCode) {
                case Constant.TO_OPEN_GPS_REQUEST_CODE:
                    //取消开启GPS
                    popupMsg("请开启GPS,否则APP将获取不到定位信息");
                    break;
            }
        }
    }

    private void initGps() {
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            showWarnDialog("请打开手机GPS定位，\n否则APP将不能正常使用", view -> {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, Constant.TO_OPEN_GPS_REQUEST_CODE);
                hideWarnDialog();
            });
        } else {
            //初始化定位sdk
               MainApp.getInstance().initMap();
        }
    }




    protected void onPermissionRequestResult(int requestCode, boolean isGranted) {
        if (isGranted) {
            MainApp.getInstance().initMap();
        } else {
            popupMsg("请给予相关的权限，否则APP将不能正常使用");
        }
    }


}